package com.whfc.fuum.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.fuum.dao.SysWorkAreaMapper;
import com.whfc.fuum.dto.SysWorkAreaDTO;
import com.whfc.fuum.entity.SysWorkArea;
import com.whfc.fuum.param.SysWorkAreaAddParam;
import com.whfc.fuum.param.SysWorkAreaEditParam;
import com.whfc.fuum.service.SysWorkAreaService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-09-01 14:40
 */
@DubboService(interfaceClass = SysWorkAreaService.class, version = "1.0.0")
public class SysWorkAreaServiceImpl implements SysWorkAreaService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysWorkAreaMapper sysWorkAreaMapper;

    @Override
    public PageData<SysWorkAreaDTO> list(Integer deptId, Integer pageNum, Integer pageSize, String keyword) throws BizException {
        logger.info("获取工区列表，deptId:{}, pageNum:{}, pageSize:{}, keyword:{}", deptId, pageNum, pageSize, keyword);
        PageHelper.startPage(pageNum, pageSize);
        List<SysWorkArea> list = sysWorkAreaMapper.selectWorkAreaList(deptId, keyword);
        PageHelper.clearPage();
        List<SysWorkAreaDTO> workAreaList = workAreaConvert(list);
        return PageUtil.pageData(PageInfo.of(workAreaList));
    }


    @Override
    public List<SysWorkAreaDTO> list(Integer deptId, String keyword) throws BizException {
        List<SysWorkArea> list = sysWorkAreaMapper.selectWorkAreaList(deptId, keyword);
        return workAreaConvert(list);
    }

    @Override
    public void add(SysWorkAreaAddParam param) throws BizException {
        logger.info("新增工区, params:{}", param);
        SysWorkArea workArea = new SysWorkArea();
        Integer deptId = param.getDeptId();
        workArea.setDeptId(deptId);
        String name = param.getName();
        SysWorkArea sysWorkArea = sysWorkAreaMapper.selectByName(deptId, name);
        if (sysWorkArea != null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "该名称已存在");
        }
        workArea.setName(name);
        param.setName(null);
        param.setDeptId(null);
        String detail = JSON.toJSONString(param);
        workArea.setDetail(detail);
        sysWorkAreaMapper.insertSelective(workArea);
    }

    @Override
    public void edit(SysWorkAreaEditParam param) throws BizException {
        logger.info("修改工区, params:{}", param);
        Integer workAreaId = param.getWorkAreaId();
        SysWorkArea workArea = sysWorkAreaMapper.selectByPrimaryKey(workAreaId);
        if (workArea == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "该工区不存在");
        }
        String name = param.getName();
        if (!workArea.getName().equals(name)) {
            SysWorkArea sysWorkArea = sysWorkAreaMapper.selectByName(workArea.getDeptId(), name);
            if (sysWorkArea != null) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "该名称已存在");
            }
        }
        workArea.setName(name);
        param.setWorkAreaId(null);
        param.setName(null);
        String detail = JSON.toJSONString(param);
        workArea.setDetail(detail);
        sysWorkAreaMapper.updateByPrimaryKeySelective(workArea);
    }

    @Override
    public void del(Integer workAreaId) throws BizException {
        logger.info("删除工区, workAreaId:{}", workAreaId);
        sysWorkAreaMapper.logicDel(workAreaId);
    }

    public SysWorkArea selectByPrimaryKey(Integer id) {
        return sysWorkAreaMapper.selectByPrimaryKey(id);
    }


    /**
     * 数据转换
     *
     * @param list 工区原始数据集合
     * @return 工区数据集合
     */
    private List<SysWorkAreaDTO> workAreaConvert(List<SysWorkArea> list) {
        List<SysWorkAreaDTO> workAreaList = new ArrayList<>();
        if (list == null || list.isEmpty()) {
            return workAreaList;
        }
        for (SysWorkArea sysWorkArea : list) {
            String detail = sysWorkArea.getDetail();
            SysWorkAreaDTO workArea = new SysWorkAreaDTO();
            if (StringUtils.isNotBlank(detail)) {
                workArea = JSON.parseObject(detail, SysWorkAreaDTO.class);
            }
            workArea.setWorkAreaId(sysWorkArea.getId());
            workArea.setName(sysWorkArea.getName());
            workAreaList.add(workArea);
        }
        return workAreaList;
    }


}
